import { createAlova } from 'alova'
import GlobalFetch from 'alova/GlobalFetch'
import VueHook from 'alova/vue'
import type { Method } from 'alova'
import { beforeRequest, onSuccess, onError } from './interceptors'

export const alovaInstance = createAlova({
  baseURL: import.meta.env.VITE_BASE_API,
  statesHook: VueHook,
  requestAdapter: GlobalFetch(),
  shareRequest: true,
  /** 请求超时时间 */
  timeout: 60 * 1000,
  // 默认关闭全部请求缓存
  localCache: null,
  // 请求拦截器
  beforeRequest,
  // 响应拦截器
  responded: {
    onSuccess,
    onError
  }
})

/**
 * @description 忽略 token
 */
export const ignoreToken = (instance: Method) => {
  if (!instance.meta) {
    instance.meta = {}
  }
  instance.meta.ignoreToken = true
  return instance
}
